﻿using LayUIFileDemo.Handler;
using LayUIFileDemo.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting.Internal;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace LayUIFileDemo.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        ///文件列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult FileList()
        {
            //演示数据
            PagedResult<FileEntity> rtnResult = new PagedResult<FileEntity>(1,
                new List<FileEntity>()
                {
                    //new FileEntity{ OriginalFileName="演示数据.txt",FileSize="20kb",FileType="txt",DisplayUrl="http:xxxxx.txt"}
                });
            //扫描服务器文件获取(也可以存数据库的)
            DirectoryInfo TheFolder = new DirectoryInfo(Path.Combine(Directory.GetCurrentDirectory(), "Resource"));
            foreach (FileInfo NextFile in TheFolder.GetFiles())
            {
                rtnResult.CurrentPageData.Add(
                    new FileEntity
                    {
                        OriginalFileName = NextFile.Name,
                        FileSize = $"{NextFile.Length / 1024}KB",
                        FileType = NextFile.Extension,
                        DisplayUrl = HttpContext.Request.GetRequestUrl()+ "/Resource/" + NextFile.Name
                    });
            }
                

            return Json(rtnResult);
        }


        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public  async Task<IActionResult> Uplaod(List<IFormFile> file)
        {
            DataResult<string> rtnResult = new DataResult<string>();

            foreach (var formFile in file)
            {
                if (formFile.Length > 0)
                {
                    FileInfo fi = new FileInfo(formFile.FileName);
                    string ext = fi.Extension;
                    var orgFileName = fi.Name;
                    //var newFileName = Guid.NewGuid() + ext;
                    var newFileName = DateTime.Now.ToString("yyyyMMddhhmmss")+formFile.FileName;

                    var uploads = Path.Combine(Directory.GetCurrentDirectory(),"Resource");
                    var filePath = Path.Combine(uploads, newFileName);
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await formFile.CopyToAsync(stream);
                    }
                    rtnResult.IsSuccess = true;
                }
                else
                {

                    rtnResult.ErrorMessage ="上传文件出错!";
                }
            }
            return Json(rtnResult);
        }


        /// <summary>
        /// 文件删除
        /// </summary>
        /// <returns></returns>
        public IActionResult Delete(string fileName)
        {

            var fullpath = Path.Combine(Directory.GetCurrentDirectory(), "Resource")+ "\\"+ fileName;

            if (System.IO.File.Exists(fullpath))
            {
                System.IO.File.Delete(fullpath);
            }

            DataResult<string> rtnResult = new DataResult<string>(data: "删除成功");
            rtnResult.IsSuccess = true;

            return Json(rtnResult);
        }
    }
}
